package com.scs.application.modules.fsd.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.fsd.mapper.BaoYiMapper;
import com.scs.application.modules.fsd.service.BaoYiService;
import com.scs.application.modules.fsd.service.SPDService;
import com.scs.application.modules.fsd.vo.SPDResponseResult;
import com.scs.application.modules.fsd.vo.StockResult;
import com.scs.application.modules.fsd.vo.StorageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.sys.security.UserPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class BaoYiServiceImpl extends SPDServiceImpl implements BaoYiService {
    @Autowired
    private SPDService spdService;

    @Autowired
    private BaoYiMapper baoYiMapper;

    @Override
    public SPDResponseResult lowConsume(String stationCode, List<String> rfidList) {
        UserPrincipal currentUser = UserUtils.currentUser();
        List<StockResult> stockResults = spdService.getStockByListRfid(rfidList);
        StorageResult storageResult = spdService.getStorageByStorageCode(stationCode);
        if (storageResult == null) {
            throw new BusinessException("站点编号在SPD中不存在！");
        }
        List<Map<String, String>> rfids = new ArrayList<>();
        SPDResponseResult spdResponseResult = new SPDResponseResult();
        for (StockResult stock : stockResults) {
            Map<String, String> sto = new HashMap<>();
            sto.put("rfid", stock.getRfid());
            sto.put("matrName", stock.getMaterialName());
            rfids.add(sto);
        }
        JSONObject consume = JSONObject.parseObject(spdService.consume(stationCode, rfids, "0.5", "fsd100", currentUser.getWorkNo()));
        spdResponseResult.setCode(consume.getInteger("code"));
        spdResponseResult.setData(consume.getString("data"));
        spdResponseResult.setMsg(consume.getString("msg"));
        return spdResponseResult;
    }

    @Override
    public Object getScanResult(String stationCode, List<String> codeList) {
       // List<StockResult> stockResults = spdService.getStockByListRfid(codeList);
        List<StockResult> stockResults = spdService.getStockByListSn(codeList);
        return stockResults;
    }

    @Override
    public List<Map<String, Object>> getLowStockByStationCode(String stationCode) {
        return  baoYiMapper.getLowStock(stationCode);
    }

    @Override
    public SPDResponseResult lowsubmit(String stationCode, String business, List<String> rfidList) {
        UserPrincipal currentUser = UserUtils.currentUser();
        //List<StockResult> stockResults = spdService.getStockByListRfid(rfidList);
        StorageResult storageResult = spdService.getStorageByStorageCode(stationCode);
        if (storageResult == null) {
            throw new BusinessException("站点编号在SPD中不存在！");
        }
        List<Map<String, String>> rfids = new ArrayList<>();
        SPDResponseResult spdResponseResult = new SPDResponseResult();
        for (String rfid : rfidList) {
            Map<String, String> sto = new HashMap<>();
            sto.put("rfid", rfid);
            sto.put("matrName", "");
            rfids.add(sto);
        }
        //   switch (business) {
        //       case "2box_Replenishment":
        String wiType ="WI";
        RestResult res = spdService.wiNotify(stationCode, wiType, rfids, "0.5", "fsd100", currentUser.getWorkNo());
        spdResponseResult.setCode(res.getCode());
        spdResponseResult.setData(String.valueOf(res.getData()));
        spdResponseResult.setMsg(res.getMsg());
//                spdResponseResult.setSuccess(res.getSuccess());
        //             break;
        //      }
        return spdResponseResult;
    }

    @Override
    public List<Map<String, Object>> getConsumeStockByStationCode(String stationCode)
    {
        return  baoYiMapper.getLowconsumeStock(stationCode);
    }
}
